package com.activiti.service;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;

/**
 * Created by oracle on 2017-03-11.
 */
@Service
public class ActivitiServiceImpl implements ActivitiService {
    @Autowired
    ProcessEngineConfiguration processEngineConfiguration;
    @Autowired
    TaskService taskService;
    @Autowired
    ProcessEngine processEngine;
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    HistoryService historyService;
    @Value("${activiti.process.name}")
    String proccessName;

    @Override
    public void testActivity() {
        List<Deployment> deployments = repositoryService.createDeploymentQuery().deploymentName(proccessName).list();
        for (Deployment deployment : deployments) {
            String name = deployment.getName();
            System.out.println("name = " + name);
        }
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(proccessName);
        List<Task> kermit = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
        for (Task task : kermit) {
            taskService.complete(task.getId());
        }
        kermit = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
        for (Task task : kermit) {
            taskService.complete(task.getId());
        }
        kermit = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
        for (Task task : kermit) {
            taskService.complete(task.getId());
        }


    }
}
